package cn.tedu.mintmall.controller;

import cn.tedu.mintmall.dto.AdminDTO;
import cn.tedu.mintmall.dto.AdminLoginDTO;
import cn.tedu.mintmall.restful.JsonResult;
import cn.tedu.mintmall.restful.ResponseCode;
import cn.tedu.mintmall.service.IAdminService;
import cn.tedu.mintmall.vo.AdminDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
/*管理员登录功能,注销功能*/
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员管理")
public class AdminController {
    @Autowired
    IAdminService adminService;

    /*登陆成功得方法 */
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号",name = "username",dataType = "String"),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String")
    })
    public JsonResult AdminLogin(AdminLoginDTO adminLoginDTO, HttpSession session){
       adminService.login(adminLoginDTO,session);
       return JsonResult.ok("登陆成功");
    }

    /*登陆成功时返回此方法可以获得此时登陆成功得管理员信息*/
    @RequestMapping("/currentUser")
    public AdminDetailsVO currentUser(HttpSession session){
        //把登陆成功时保存的用户对象 返回给客户端
        return (AdminDetailsVO) session.getAttribute("admin");
    }

    /*注销登录得方法*/
    @GetMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("admin");
    }

    @PostMapping("/update")
    public JsonResult updateAdmin(String password,HttpSession session){
        adminService.updateAdmin(password,session);
        session.removeAttribute("admin");
        return JsonResult.ok("修改完成,请重新登录");

    }
}
